package com.cn.yang.service.impl;

import com.cn.yang.dao.ProductDao;
import com.cn.yang.dao.impl.ProductDaoImpl;
import com.cn.yang.model.production.ProductClassifyModel;
import com.cn.yang.model.production.ProductModel;
import com.cn.yang.service.ProductService;
import com.cn.yang.util.PageUtil;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;

public class ProductServiceImpl implements ProductService {
    //查询产品种类===========
    @Override
    public List<ProductClassifyModel> findClassify() {
        ProductDao productDao = new ProductDaoImpl();
        Jedis jedis = new Jedis();
        long length = jedis.llen("productClassify_id");
        List<ProductClassifyModel> list = new ArrayList<>();
        if (length>0){
            List<String> productClassify_id = jedis.lrange("productClassify_id",0,-1);
            List<String> productClassify_name = jedis.lrange("productClassify_name",0,-1);
            for (int i =0;i<length;i++){
                ProductClassifyModel model = new ProductClassifyModel();
                model.setId(Integer.parseInt(productClassify_id.get(i)));
                model.setName(productClassify_name.get(i));
                list.add(model);
            }
        }else {
            list = productDao.findClassify();
            for (int i = 0;i < list.size();i++){
                jedis.rpush("productClassify_id", String.valueOf(list.get(i).getId()));
                jedis.rpush("productClassify_name",list.get(i).getName());
            }
        }
        return list;
    }
//添加产品信息========
    @Override
    public int addProduct(ProductModel model) {
        ProductDao productDao = new ProductDaoImpl();
        Jedis jedis = new Jedis();
        jedis.del("product_id");
        jedis.del("product_img");
        jedis.del("product_name");
        jedis.del("product_classifyName");
        jedis.del("product_diliver");
        jedis.del("product_content");
        jedis.del("product_averageprice");
        jedis.del("product_price");
        jedis.del("product_quality");
        jedis.del("product_payment");
        jedis.del("product_information");
        return productDao.addProduct(model);
    }
//查找 产品信息=========
    @Override
    public List<ProductModel> findProduct(ProductModel model) {
        ProductDao productDao = new ProductDaoImpl();
        List<ProductModel> list = new ArrayList<>();
        //只有当 tpye == null 和 pageNumber（数据库的pagenumber）为0的时候才走redis 否住 全走数据库
        if (model.getPageUtil().getPageNumber()==0 && model.getClassify()==null && model.getSortUserUtil().getSortClassifyName()==null){
            Jedis jedis = new Jedis();
            long lenght = jedis.llen("product_id");
            if (lenght>0){//有值 走redis
                System.out.println("走redis");
                List<String> product_id = jedis.lrange("product_id",0,-1);
                List<String> product_img = jedis.lrange("product_img",0,-1);
                List<String> product_name = jedis.lrange("product_name",0,-1);
                List<String> product_classifyName = jedis.lrange("product_classifyName",0,-1);
                List<String> product_diliver = jedis.lrange("product_diliver",0,-1);
    
                List<String> product_content = jedis.lrange("product_content",0,-1);
                List<String> product_averageprice = jedis.lrange("product_averageprice",0,-1);
                List<String> product_price = jedis.lrange("product_price",0,-1);
                List<String> product_quality = jedis.lrange("product_quality",0,-1);
                List<String> product_payment = jedis.lrange("product_payment",0,-1);
                List<String> product_information = jedis.lrange("product_information",0,-1);
                for (int i = 0;i<lenght;i++){
                    ProductModel productModel = new ProductModel();
                    productModel.setId(Integer.parseInt(product_id.get(i)));
                    productModel.setImg(product_img.get(i));
                    productModel.setName(product_name.get(i));
                    productModel.setClassifyName(product_classifyName.get(i));
                    productModel.setDiliver(product_diliver.get(i));
                    
                    productModel.setContent(product_content.get(i));
                    productModel.setAverageprice(product_averageprice.get(i));
                    productModel.setPrice(product_price.get(i));
                    productModel.setQuality(product_quality.get(i));
                    productModel.setPayment(product_payment.get(i));
                    productModel.setInformation(product_information.get(i));
                    list.add(productModel);
                }
            }else {
                list = productDao.findProduct(model);
                for (int i= 0;i<list.size();i++){
                    jedis.rpush("product_id",String.valueOf(list.get(i).getId()));
                    jedis.expire("product_id",30*60);
                    jedis.rpush("product_img",list.get(i).getImg());
                    jedis.expire("product_img",30*60);
                    jedis.rpush("product_name",list.get(i).getName());
                    jedis.expire("product_name",30*60);
                    jedis.rpush("product_classifyName",String.valueOf(list.get(i).getClassifyName()));
                    jedis.expire("product_classifyName",30*60);
                    jedis.rpush("product_diliver",list.get(i).getDiliver());
                    jedis.expire("product_diliver",30*60);
    
                    jedis.rpush("product_content",list.get(i).getContent());
                    jedis.expire("product_content",30*60);
                    jedis.rpush("product_averageprice",list.get(i).getAverageprice());
                    jedis.expire("product_averageprice",30*60);
                    jedis.rpush("product_price",list.get(i).getPrice());
                    jedis.expire("product_price",30*60);
                    jedis.rpush("product_quality",list.get(i).getQuality());
                    jedis.expire("product_quality",30*60);
                    jedis.rpush("product_payment",list.get(i).getPayment());
                    jedis.expire("product_payment",30*60);
                    jedis.rpush("product_information",list.get(i).getInformation());
                    jedis.expire("product_information",30*60);
                }
            }
        }else {
            list=productDao.findProduct(model);
        }
        return list;
    }
    // public List<ProductModel> findProduct(Integer classify) {
    //     ProductDao productDao = new ProductDaoImpl();
    //     return productDao.findProduct(classify);
    // }
//产品总数
    @Override
    public int countProduct(ProductModel model) {
        ProductDao productDao = new ProductDaoImpl();
        return productDao.countProduct(model);
    }
//删除某个产品
    @Override
    public int deleteById(Integer id) {
        ProductDao productDao = new ProductDaoImpl();
        Jedis jedis = new Jedis();
        jedis.del("product_id");
        jedis.del("product_img");
        jedis.del("product_name");
        jedis.del("product_classifyName");
        jedis.del("product_diliver");
        jedis.del("product_content");
        jedis.del("product_averageprice");
        jedis.del("product_price");
        jedis.del("product_quality");
        jedis.del("product_payment");
        jedis.del("product_information");
        return productDao.deleteById(id);
    }

    @Override
    public ProductModel selectById(Integer id) {
        ProductDao productDao = new ProductDaoImpl();
        return productDao.selectById(id);
    }
//更新product
    @Override
    public int updateProduct(ProductModel model) {
        ProductDao productDao = new ProductDaoImpl();
        Jedis jedis = new Jedis();
        jedis.del("product_id");
        jedis.del("product_img");
        jedis.del("product_name");
        jedis.del("product_classifyName");
        jedis.del("product_diliver");
        jedis.del("product_content");
        jedis.del("product_averageprice");
        jedis.del("product_price");
        jedis.del("product_quality");
        jedis.del("product_payment");
        jedis.del("product_information");
        return productDao.updateProduct(model);
    }
}
